package com.chance.cc.crawler.development.bootstrap.wangyiyun;

import com.chance.cc.crawler.core.CrawlerEnum;
import com.chance.cc.crawler.core.downloader.HttpConfig;
import com.chance.cc.crawler.core.downloader.HttpRequest;
import com.chance.cc.crawler.core.downloader.proxy.Proxy;
import com.chance.cc.crawler.core.filter.FilterUtils;
import com.chance.cc.crawler.core.record.CrawlerRequestRecord;
import com.chance.cc.crawler.development.controller.DevCrawlerController;
import org.apache.commons.lang3.StringUtils;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import static com.chance.cc.crawler.core.CrawlerEnum.CrawlerRequestType.turnPage;

/**
 * @Author Zhao.Hhuan
 * @Date Create in 2021/3/22 13:27
 * @Description
 **/
public class WangYiYun {
    private static String domain = "wangyiyun";

    private static Proxy proxy = new Proxy();
    static {
        //代理配置
        //HL89Q19E86E2987D
        //71F33D94CE5F7BF2
        proxy.setHost("http-dyn.abuyun.com");
        proxy.setPort(9020);
        proxy.setUsername("HL89Q19E86E2987D");
        proxy.setPassword("71F33D94CE5F7BF2");
    }

    public static void main(String[] args) {
        itemRecordTest();
    }

    private static void itemRecordTest(){
        String url = null;
        try {
            url = "http://music.163.com/api/search/pc?s="+ URLEncoder.encode("古风","UTF-8")+"&limit=100&type=1000&offset=0";
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        String site = "searchKw";
        String siteBiz = "search";
        CrawlerRequestRecord crawlerRequestRecord = CrawlerRequestRecord.builder()
                .startPageRequest(domain, turnPage)
                .domain(domain)
                .httpUrl(url)
                .httpConfig(HttpConfig.me(domain))
                .filter(CrawlerEnum.CrawlerRecordFilter.key)
                .addFilterInfo(FilterUtils.memoryFilterKeyInfo(StringUtils.joinWith("filter",domain,site,siteBiz,"queue")))
                .releaseTime(System.currentTimeMillis())
                .needWashed(true)
                .resultLabelTag(CrawlerEnum.CrawlerDataType.article)
                .proxy(proxy)
                .build();
        crawlerRequestRecord.tagsCreator().bizTags().addDomain(domain);
        crawlerRequestRecord.tagsCreator().bizTags().addSite(site);
        crawlerRequestRecord.tagsCreator().bizTags().addSiteBiz(siteBiz);
        crawlerRequestRecord.tagsCreator().bizTags().addKeywords("古风");

        HttpRequest httpRequest = crawlerRequestRecord.getHttpRequest();
        httpRequest.addHeader("Referer","http://music.163.com/");
        httpRequest.addHeader("Cookie","appver=1.5.0.75771");

        DevCrawlerController.builder()
                .crawlerThreadNum(5)
                .triggerInfo(domain,domain,System.currentTimeMillis(),domain)
                .crawlerRequestQueue(DevCrawlerController.devRequestQueue(domain))
                .consoleResultPipeline()//控制台输出
                .fileResultPipeline("D:\\chance\\log\\wangyiyun_test.log",false) //文件输出
                .requestRecord(crawlerRequestRecord)
                .build()
                .start();
    }
}
